<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f5f5f5;
            margin: 0;
        }
        .login-container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        .login-container h1 {
            margin-top: 0;
            text-align: center;
        }
        .login-container label {
            display: block;
            margin-top: 15px;
            font-weight: bold;
        }
        .login-container input[type="text"],
        .login-container input[type="password"] {
            width: 100%;
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .captcha-container {
            display: flex;
            align-items: center;
            margin-top: 15px;
        }
        .captcha-container img {
            margin-left: 10px;
            cursor: pointer;
        }
        .login-container input[type="submit"] {
            margin-top: 20px;
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            border: none;
            border-radius: 4px;
            color: #ffffff;
            font-size: 16px;
            cursor: pointer;
        }
        .login-container input[type="submit"]:hover {
            background-color: #0056b3;
        }
        .error-message {
            color: red;
            text-align: center;
            margin-top: 15px;
        }
    </style>
    <!-- 引入 jsSHA 库 -->
    <script src="https://cdn.jsdelivr.net/npm/jssha/dist/sha256.min.js"></script>
</head>
<body>
    <div class="login-container">
        <h1>登录</h1>
        <form method="post" onsubmit="return hashPassword();">
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" required>

            <label for="password">密码：</label>
            <input type="password" id="password" name="password" required>

            <label for="captcha">验证码：</label>
            <div class="captcha-container">
                <input type="text" id="captcha" name="captcha" required>
                <img src="{{ url_for('get_captcha') }}" alt="验证码" onclick="this.src='{{ url_for('get_captcha') }}?t=' + Math.random();">
            </div>

            <input type="submit" value="登录">
        </form>
        {% with messages = get_flashed_messages() %}
          {% if messages %}
            <div class="error-message">
              {% for message in messages %}
                <p>{{ message }}</p>
              {% endfor %}
            </div>
          {% endif %}
        {% endwith %}
    </div>

    <script>
        function hashPassword() {
            var passwordField = document.getElementById('password');
            var password = passwordField.value;

            // 使用 jsSHA 进行 SHA-256 哈希
            var shaObj = new jsSHA("SHA-256", "TEXT");
            shaObj.update(password);
            var hash = shaObj.getHash("HEX");

            // 将哈希值赋值回密码字段，并隐藏密码明文
            passwordField.value = hash;

            return true; // 继续提交表单
        }
    </script>
</body>
</html>
